package club.kingyin.kyblogprofile.entity;

import club.kingyin.commons.utils.SizeUtils;
import lombok.Data;

import java.sql.Timestamp;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.regex.Pattern;

@Data
public class Folder {

    private ArrayList<CloudFile> files = new ArrayList<>();

    public void add(String name, double size, ZonedDateTime last, boolean world) {
        CloudFile file = new CloudFile();
        file.setSize(SizeUtils.changeSize(size));
        file.setType(FileType.getType(name));
        file.setPath(name);
        if (name.contains("/")) {
            String[] split = name.split("/");
            name = split[split.length - 1];
        }
        if (file.getType() != FileType.FOLDER) {
            int suffixIndex = name.lastIndexOf(".");
            file.setSuffix(name.substring(suffixIndex+1));
            name =  name.substring(0,suffixIndex);
        }
        file.setName(name);
        if (last != null)
            file.setLastModify(Timestamp.from(last.toInstant()).toString());
        if (world) {
            String pattern = "^\\d{7}$";
            if (Pattern.matches(pattern, file.getName())) {
                files.add(file);
            }
        } else
            files.add(file);
    }

}
